#include <iostream>
#include <mysql/mysql.h>
#include <string>
#include <vector>
#include <map>

// 初始化数据库连接
MYSQL *initDatabase(const std::string &host, const std::string &user, const std::string &password, const std::string &db) {
    MYSQL *conn = mysql_init(nullptr);
    if (conn == nullptr) {
        std::cerr << "mysql_init() failed" << std::endl;
        return nullptr;
    }
    if (mysql_real_connect(conn, host.c_str(), user.c_str(), password.c_str(), db.c_str(), 0, nullptr, 0) == nullptr) {
        std::cerr << "mysql_real_connect() failed: " << mysql_error(conn) << std::endl;
        mysql_close(conn);
        return nullptr;
    }
    return conn;
}

// 执行查询
void executeQuery(MYSQL *conn, const std::string &query) {
    if (mysql_query(conn, query.c_str())) {
        std::cerr << "mysql_query() failed: " << mysql_error(conn) << std::endl;
    }
}

// 用户注册
void registerUser(MYSQL *conn) {
    std::string username, password;
    std::cout << "请输入用户名: ";
    std::cin >> username;
    std::cout << "请输入密码: ";
    std::cin >> password;

    std::string hashedPassword = password; 
    std::string query = "INSERT INTO users (username, password) VALUES ('" + username + "', '" + hashedPassword + "')";
    executeQuery(conn, query);
}

// 用户登录
bool loginUser(MYSQL *conn) {
    std::string username, password;
    std::cout << "请输入用户名: ";
    std::cin >> username;
    std::cout << "请输入密码: ";
    std::cin >> password;

    std::string query = "SELECT * FROM users WHERE username='" + username + "' AND password='" + password + "'";
    executeQuery(conn, query);

    MYSQL_RES *res = mysql_store_result(conn);
    bool result = mysql_num_rows(res) > 0;
    mysql_free_result(res);
    return result;
}

// 显示书籍列表
void listBooks(MYSQL *conn) {
    std::string query = "SELECT title, id FROM books";
    executeQuery(conn, query);
}

// 显示章节内容
void showChapter(MYSQL *conn, int chapterId) {
    std::string query = "SELECT content FROM chapters WHERE id=" + std::to_string(chapterId);
    executeQuery(conn, query);
}

// 点赞章节
void likeChapter(MYSQL *conn, int chapterId) {
    std::string query = "UPDATE chapters SET likes = likes + 1 WHERE id=" + std::to_string(chapterId);
    executeQuery(conn, query);
}

// 编辑章节内容
void editChapter(MYSQL *conn, int chapterId, const std::string &newContent) {
    std::string query = "UPDATE chapters SET content = '" + newContent + "' WHERE id=" + std::to_string(chapterId);
    executeQuery(conn, query);
}

// 上传新章节内容
void uploadChapter(MYSQL *conn, int bookId, const std::string &content) {
    std::string query = "INSERT INTO chapters (book_id, content) VALUES (" + std::to_string(bookId) + ", '" + content + "')";
    executeQuery(conn, query);
}

// 上传书籍
void uploadBook(MYSQL *conn, const std::string &title, const std::string &author) {
    std::string query = "INSERT INTO books (title, author) VALUES ('" + title + "', '" + author + "')";
    executeQuery(conn, query);
}

int main() {
    MYSQL *conn = initDatabase("hostname", "username", "password", "zdds");//数据库名称是zdds
    if (conn == nullptr) {
        return 1;
    }

    std::string choice;
    do {
        std::cout << "欢迎使用终点读书！\n";
        std::cout << "1. 用户注册\n";
        std::cout << "2. 用户登录\n";
        std::cout << "3. 查看书籍列表\n";
        std::cout << "4. 查看章节内容\n";
        std::cout << "5. 点赞章节\n";
        std::cout << "6. 编辑章节内容\n";
        std::cout << "7. 上传新章节内容\n";
        std::cout << "8. 上传书籍\n";
        std::cout << "0. 退出\n";
        std::cout << "请选择操作: ";
        std::cin >> choice;

        if (choice == "1") {
            registerUser(conn);
        } else if (choice == "2") {
            if (loginUser(conn)) {
                std::cout << "登录成功！\n";
            } else {
                std::cout << "登录失败！\n";
            }
        } else if (choice == "3") {
            listBooks(conn);
        } else if (choice == "4") {
            int chapterId;
            std::cout << "请输入章节ID: ";
            std::cin >> chapterId;
            showChapter(conn, chapterId);
        } else if (choice == "5") {
            int chapterId;
            std::cout << "请输入章节ID: ";
            std::cin >> chapterId;
            likeChapter(conn, chapterId);
        } else if (choice == "6") {
            int chapterId;
            std::cout << "请输入章节ID: ";
            std::cin >> chapterId;
            std::string newContent;
            std::cout << "请输入新的章节内容: ";
            std::getline(std::cin, newContent); // 使用getline来读取整行内容，包括空格
            editChapter(conn, chapterId, newContent);
        } else if (choice == "7") {
            int bookId;
            std::cout << "请输入书籍ID: ";
            std::cin >> bookId;
            std::string content;
            std::cout << "请输入章节内容: ";
            std::getline(std::cin, content); // 使用getline来读取整行内容，包括空格
            uploadChapter(conn, bookId, content);
        } else if (choice == "8") {
            std::string title, author;
            std::cout << "请输入书籍标题: ";
            std::getline(std::cin, title);
            std::cout << "请输入作者名: ";
            std::getline(std::cin, author);
            uploadBook(conn, title, author);
        } else if (choice == "0") {
            std::cout << "感谢使用终点读书，再见！\n";
        } else {
            std::cout << "无效的选择，请重新输入。\n";
        }
    } while (choice != "0");

    mysql_close(conn);
    return 0;
}
